<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxActivity;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxActivityController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxActivity(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'title', 'intro'])->placeholder('搜索活动id，标题，介绍...')->width(35);
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('intro');
            $grid->column('pic')->image('',80);
            $grid->column('target_type')->using(FieldUtils::getUrlTypes());
            $grid->column('target_id');
            $grid->column('status')->using([0=>'正常',1=>'活动结束'])->label([
                0 => 'success',
                1 => 'red',
            ]);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxActivity(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('intro');
            $show->field('pic');
            $show->field('target_type');
            $show->field('target_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxActivity(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->textarea('intro');
            $form->image('pic')->url('files/uploads')->uniqueName()->autoUpload();
            $form->radio('target_type')->options(FieldUtils::getUrlTypes())->default(0)->required();
            $form->text('target_id', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            $form->radio('status')->options([0=>'正常',1=>'活动结束'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
